var page = sm("do_Page");
var app = sm("do_App");
var storage = sm("do_InitData");
var open = require("open");
var nf = sm("do_Notification");
var rootview = ui("$");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");
var buttonA = mm("do_Animation", "BUTTONTOUCHDOWNS", "app");
var param = {bigCategory:[],secondCategory:[],selectedCategory:{}};

//遮罩显示
var animMaskShow = mm("do_Animator");
var propsMS = {bgColor:"00000077"};
animMaskShow.append(300,propsMS,"EaseOut");
//面板显示
var animPanelShow = mm("do_Animator");
var propsPS = {y:460};
animPanelShow.append(300,propsPS,"Linear");
//遮罩隐藏
var animMaskHide = mm("do_Animator");
var propsMH = {bgColor:"00000000"};
animMaskHide.append(300,propsMH,"EaseIn");
//面板隐藏
var animPanelHide = mm("do_Animator");
var propsPH = {y:960};
animPanelHide.append(300,propsPH,"EaseIn");

//遮罩事件
var maskbg = ui("bgmask");
maskbg.on("touch","",300,function(){
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
	bodyp.animate(animPanelHide);
});
//防止穿透
var bodyp = ui("pbody");
bodyp.on("touch","",300,function(){
	
});
//确定
var surebtn = ui("do_Button_sure");
surebtn.on("touch","",300,function(){
	if(param.selectedCategory!=""){
		page.fire("refreshCategory",param.selectedCategory);
	}
	maskbg.animate(animMaskHide,function(){
		maskbg.visible = false;
	});
	bodyp.animate(animPanelHide);
});
//zdy
page.on("categorys",function(data){
	maskbg.visible = data;
	maskbg.animate(animMaskShow);
	bodyp.animate(animPanelShow);
});

var bigCategorypick = ui("do_Picker_bigCategory");
var bigCategorydate = mm("do_ListData");
//获取大类bigCategory:[],secondCategory:[],selectedCategory:[]
page.on("categoryList",function(data){
	param.bigCategory=data;
	bigCategorydate.removeAll();
	var bCategory = [];
	for(var i=0,length=data.length;i<length;i++){
		bCategory[i] = data[i].name;
	}
	bigCategorydate.addData(bCategory);
	bigCategorypick.bindItems(bigCategorydate);
	bigCategorypick.refreshItems();
	categoryLoad(data[0].id);
	param.selectedCategory.bid = data[0].id;
	param.selectedCategory.bname = data[0].name;
	
	
});
//省滑动
bigCategorypick.on("selectChanged",function(index){
	param.selectedCategory.bid = param.bigCategory[index].id;
	param.selectedCategory.bname = param.bigCategory[index].name;
	categoryLoad(param.bigCategory[index].id);
});

var secondCategorypick = ui("do_Picker_secondCategory");
var secondCategorydata = mm("do_ListData");
///加载市
function categoryLoad(pid){
	qqHttp.post(homemakingUtil.url.type.findChildren, {parentId:pid}, function(data) {
		secondCategorydata.removeAll();
		var datas = data.childrenList;
		param.secondCategory=datas;
		var sCategory = [];
		for(var i=0;i<datas.length;i++){
			sCategory[i] = datas[i].name;
		}
		secondCategorydata.addData(sCategory);
		secondCategorypick.bindItems(secondCategorydata);
		secondCategorypick.refreshItems();
		
		param.selectedCategory.sid = datas[0].id;
		param.selectedCategory.sname = datas[0].name;
	});
}
//城市选择
secondCategorypick.on("selectChanged",function(index){
	param.selectedCategory.sid = param.secondCategory[index].id;
	param.selectedCategory.sname = param.secondCategory[index].name;
});
